﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ICSharpCode.TextEditor.Document;
using Tilver.Coder.CommandExcuteResult;
using Tilver.Coder.Core;
using Tilver.Coder.Utility;
using Tilver.Coder.Win.Dialogs;

namespace Tilver.Coder.Win.Views
{
    public partial class CommandView : UserControl
    {
        private List<CoderColumn> _columns = new List<CoderColumn>();
        private CoderCommand _command;
        public CoderCommand Command
        {
            get { return _command; }
            set
            {
                _command = value;
                LoadCommandInfo();
            }
        }

        public CommandView()
        {
            InitializeComponent();
            #region 初始化数据
            foreach (var commandResult in System.Enum.GetValues(typeof(ExecuteResultEnum)))
            {
                cmbCommandExecuteType.Items.Add(commandResult);
            }

            foreach (DbType type in System.Enum.GetValues(typeof(DbType)))
            {
                cmbCommandDbType.Items.Add(type);
            }
            #endregion
        }

        private void btnSetCommandResult_Click(object sender, EventArgs e)
        {
            var dialog = new ColumnInterpretorDialog();
            dialog.Set(_command.DataBase, _command.FullName, CommandType.StoredProcedure, _command.Parameters);
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                _columns = dialog.Columns;
                (_command.CommandResult as ExecuteSingleResult).Columns = _columns;
                columnViewResult.DataObjects = _columns;
            }
        }

        private void tabControlCommand_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControlCommand.SelectedIndex != 3 && tabControlCommand.SelectedTab.Tag != null)
                return;

            switch (tabControlCommand.SelectedIndex)
            {
                case 0:
                    LoadCommandInfo();
                    break;
                case 1:
                    LoadCommandParameters();
                    break;
                case 2:
                    LoadCommandResult();
                    break;
                case 3:
                    LoadCommandCode();
                    break;
            }

            tabControlCommand.SelectedTab.Tag = true;
        }

        /// <summary>
        /// 显示存储过程信息
        /// </summary>
        private void LoadCommandInfo()
        {
            textEditorCommand.Text = _command.CommandText;
        }
        /// <summary>
        /// 显示存储过程参数
        /// </summary>
        private void LoadCommandParameters()
        {
            columnViewParameters.DataObjects = _command.Parameters;
        }
        /// <summary>
        /// 显示存储过程执行结果
        /// </summary>
        private void LoadCommandResult()
        {
            if (_command.CommandResult == null) return;
            cmbCommandExecuteType.SelectedItem = _command.CommandResult.CommandResult;
            cmbCommandDbType.SelectedItem = DbType.Int32;
            switch (_command.CommandResult.CommandResult)
            {
                case ExecuteResultEnum.ExecuteNonQuery:
                    break;
                case ExecuteResultEnum.ExecuteScalar:
                    cmbCommandDbType.SelectedItem = (_command.CommandResult as ExecuteScalarResult).DbType;
                    break;
                case ExecuteResultEnum.ExecuteSingle:
                case ExecuteResultEnum.ExecuteReader:
                    _columns = (_command.CommandResult as ExecuteSingleResult).Columns;
                    break;
            }
            columnViewResult.DataObjects = _columns;
        }
        /// <summary>
        /// 显示存储过程代码
        /// </summary>
        private void LoadCommandCode()
        {
            var engineFactory = new Engine.EngineFactory();
            var engine = engineFactory.GetEngine(_command);
            textEditorCode.Text = engine.RenderToString();
        }

        private void cmbCommandExecuteType_SelectedIndexChanged(object sender, EventArgs e)
        {
            var commandResult = (ExecuteResultEnum)cmbCommandExecuteType.SelectedItem;
            if (commandResult != _command.CommandResult.CommandResult)
            {
                _command.CommandResult = ExecuteResultUtil.Create(commandResult);

                switch (commandResult)
                {
                    case ExecuteResultEnum.ExecuteNonQuery:
                        break;
                    case ExecuteResultEnum.ExecuteScalar:
                        DbType dbType = (DbType)cmbCommandDbType.SelectedItem;
                        ExecuteScalarResult scalar = _command.CommandResult as ExecuteScalarResult;
                        scalar.DbType = dbType;
                        scalar.CSharpType = DBTypeUtil.ConvertToCSharpType(dbType, true);
                        break;
                    case ExecuteResultEnum.ExecuteSingle:
                    case ExecuteResultEnum.ExecuteReader:
                        (_command.CommandResult as ExecuteSingleResult).Columns = _columns;
                        break;
                }
            }

            switch (commandResult)
            {
                case ExecuteResultEnum.ExecuteNonQuery:
                    cmbCommandDbType.Enabled = false;
                    //pvColumn.EditMode = EditMode.Readonly;
                    btnSetCommandResult.Enabled = false;
                    break;
                case ExecuteResultEnum.ExecuteScalar:
                    cmbCommandDbType.Enabled = true;
                    //pvColumn.EditMode = EditMode.Readonly;
                    btnSetCommandResult.Enabled = false;
                    break;
                case ExecuteResultEnum.ExecuteSingle:
                case ExecuteResultEnum.ExecuteReader:
                    cmbCommandDbType.Enabled = false;
                    //pvColumn.EditMode = EditMode.All;
                    btnSetCommandResult.Enabled = true;
                    break;
            }
        }

        private void cmbCommandDbType_SelectedIndexChanged(object sender, EventArgs e)
        {
            var commandResult = (ExecuteResultEnum)cmbCommandExecuteType.SelectedItem;

            if (commandResult != ExecuteResultEnum.ExecuteScalar) return;
            var scalar = (_command.CommandResult as ExecuteScalarResult);
            scalar.DbType = (DbType)cmbCommandDbType.SelectedItem;
            scalar.CSharpType = DBTypeUtil.ConvertToCSharpType(scalar.DbType, true);
        }
    }
}
